﻿using System.Linq;
using Database.Context;
using QuanLyThongTinCoSo.BaseForm;
using QuanLyThongTinCoSo.Biz;
using QuanLyThongTinCoSo.Common;
using Model = Database.Models;

namespace QuanLyThongTinCoSo.Forms.PhieuDieuTraCoSo
{
    public partial class frmNguonNuoc : frmDetail
    {
        public frmNguonNuoc()
        {
            InitializeComponent();
            Text = @"Thêm Nguồn Nước";
            txtTen.Focus();
        }

        public frmNguonNuoc(int id)
        {
            InitializeComponent();
            Text = @"Sửa Thẩm duyệt PCCC";
            txtTen.Focus();
            _fromtype = Constant.TypeForm.Edit;
            _id = id;

            var biz = new NguonNuocBiz();
            var item = biz.GetItemById(id) as Model.NguonNuoc;
            if (item == null) return;

            drlLoaiNguonNuoc.SelectedValue = item.PhanLoaiChiTietNguonNuoc.LoaiNguonNuoc.Id;
            txtTen.Text = item.Ten;
            drlPhanLoaiChiTietNguonNuoc.SelectedValue = item.PhanLoaiChiTietNguonNuoc.Id;
            txtTrongCoSo.Text = item.TrongCoSo;
            txtNgoaiCoSo.Text = item.NgoaiCoSo;
            txtKhoiTich.Text = item.KhoiTich;
            txtXeCC.Text = item.XeCC;
            txtMBCC.Text = item.MBCC;
        }

        private void frmNguonNuoc_Load(object sender, System.EventArgs e)
        {
            LoadLoaiNguonNuocBiz();
        }

        private void LoadLoaiNguonNuocBiz()
        {
            var biz = new LoaiNguonNuocBiz();
            drlLoaiNguonNuoc.DataSource = biz.GetAll();
            drlLoaiNguonNuoc.DisplayMember = "Ten";
        }

        private void drlLoaiNguonNuoc_SelectedValueChanged(object sender, System.EventArgs e)
        {
            var item = drlLoaiNguonNuoc.SelectedValue as Model.LoaiNguonNuoc;
            LoadPhanLoaiChiTietNguonNuoc(item.Id);
        }

        private void LoadPhanLoaiChiTietNguonNuoc(int id)
        {
            var biz = new PhanLoaiChiTietNguonNuocBiz();
            drlPhanLoaiChiTietNguonNuoc.DataSource = biz.GetAll().Where(m => m.LoaiNguonNuoc.Id == id).ToList();
            drlPhanLoaiChiTietNguonNuoc.DisplayMember = "Ten";
        }

        #region Validation

        protected override bool Validation()
        {
            bool flag = true;

            errorProvider.Dispose();

            if (string.IsNullOrEmpty(txtTen.Text))
            {
                errorProvider.SetError(txtTen, "Bạn chưa nhập Tên Nguồn nước. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            return flag;
        }

        #endregion

        #region Insert

        protected override void Insert()
        {
            var context = new Context();
            var item = new Model.NguonNuoc();

            var phanLoaiChiTietNguonNuocBiz = new PhanLoaiChiTietNguonNuocBiz(context);
            var model = drlPhanLoaiChiTietNguonNuoc.SelectedValue as Model.PhanLoaiChiTietNguonNuoc;
            item.PhanLoaiChiTietNguonNuoc = (Model.PhanLoaiChiTietNguonNuoc) phanLoaiChiTietNguonNuocBiz.GetItemById(model.Id);
            item.Ten = txtTen.Text.Trim();
            item.TrongCoSo = txtTrongCoSo.Text.Trim();
            item.NgoaiCoSo = txtNgoaiCoSo.Text.Trim();
            item.KhoiTich = txtKhoiTich.Text.Trim();
            item.XeCC = txtXeCC.Text.Trim();
            item.MBCC = txtMBCC.Text.Trim();
            var biz = new NguonNuocBiz(context);
            biz.Insert(item);
        }

        #endregion

        #region Edit

        protected override void Edit()
        {
            var context = new Context();
            var biz = new NguonNuocBiz(context);
            var item = biz.GetItemById(_id) as Model.NguonNuoc;
            if (item != null)
            {
                item.Ten = txtTen.Text.Trim();
                item.TrongCoSo = txtTrongCoSo.Text.Trim();
                item.NgoaiCoSo = txtNgoaiCoSo.Text.Trim();
                item.KhoiTich = txtKhoiTich.Text.Trim();
                item.XeCC = txtXeCC.Text.Trim();
                item.MBCC = txtMBCC.Text.Trim();

                var phanLoaiChiTietNguonNuocBiz = new PhanLoaiChiTietNguonNuocBiz(context);
                var itemPhanLoaiChiTiet = drlPhanLoaiChiTietNguonNuoc.SelectedValue as Model.PhanLoaiChiTietNguonNuoc;
                item.PhanLoaiChiTietNguonNuoc = (Model.PhanLoaiChiTietNguonNuoc) phanLoaiChiTietNguonNuocBiz.GetItemById(itemPhanLoaiChiTiet.Id);
                biz.Update(item);
            }
        }

        #endregion
    }
}